package com.ehome.poct.base.dao.device.repository;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ehome.exception.BizException;
import com.ehome.poct.base.api.dto.device.TestDeviceParamDTO;
import com.ehome.poct.base.api.vo.device.TestDeviceParamVO;
import com.ehome.poct.base.dao.device.entity.TestBloodDeviceParamNewDO;
import com.ehome.poct.base.dao.device.entity.TestBloodDeviceParamRecordDO;
import com.ehome.poct.base.dao.device.mapper.TestBloodDeviceParamNewMapper;
import com.ehome.poct.base.dao.device.mapper.TestBloodDeviceParamRecordMapper;
import com.ehome.poct.base.device.repository.AbstractDeviceRepository;
import com.ehome.poct.base.device.repository.TestDeviceParamRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备表RepositoryImpl
 *
 * @Date 2022-03-03 09:23
 * @since 瑶苏
 */
@Repository
@AllArgsConstructor
public class TestDeviceParamRepositoryImpl implements TestDeviceParamRepository {
    private final List<AbstractDeviceRepository> abstractDeviceRepositories;
    private final TestBloodDeviceParamNewMapper testBloodDeviceParamNewMapper;
    private final TestBloodDeviceParamRecordMapper testBloodDeviceParamRecordMapper;

    @Override
    public Boolean setDeviceParam(TestDeviceParamDTO testDeviceParamDTO) {
        TestBloodDeviceParamNewDO one = testBloodDeviceParamNewMapper.selectOne(Wrappers.<TestBloodDeviceParamNewDO>lambdaQuery()
                .eq(TestBloodDeviceParamNewDO::getDeviceSno, testDeviceParamDTO.getDeviceSno()));
        Boolean result = false;
        if (Validator.isNotNull(one)) {
            TestBloodDeviceParamNewDO testBloodDeviceParam = new TestBloodDeviceParamNewDO(testDeviceParamDTO.getDeviceParamData(), testDeviceParamDTO.getDeviceSno(), testDeviceParamDTO.getDeviceSha());
            testBloodDeviceParam.setTestBloodDeviceParamId(one.getTestBloodDeviceParamId());
            Integer upd = testBloodDeviceParamNewMapper.updateById(testBloodDeviceParam);
            if (upd == 1) {
                result = true;
            }
            
        } else {
            //添加设备
            abstractDeviceRepositories.forEach(r ->
            {
                if (r.deviceType(testDeviceParamDTO.getDeviceType())) {
                    r.addDeviceParam(testDeviceParamDTO);
                }
            });
            result = true;
        }
        
        TestBloodDeviceParamRecordDO testBloodDeviceParamRecord = new TestBloodDeviceParamRecordDO();
        testBloodDeviceParamRecord.setDeviceSno(testDeviceParamDTO.getDeviceSno());
        testBloodDeviceParamRecord.setDeviceSha(testDeviceParamDTO.getDeviceSha());
        testBloodDeviceParamRecord.setDeviceParamData(testDeviceParamDTO.getDeviceParamData());
        testBloodDeviceParamRecord.setUpdateTime(LocalDateTime.now());
        testBloodDeviceParamRecord.setOperator(testDeviceParamDTO.getOperator());
        
        testBloodDeviceParamRecordMapper.insert(testBloodDeviceParamRecord);
        return result;
    }
    
    @Override
    public TestDeviceParamVO getDeviceParamNew(TestDeviceParamDTO testDeviceParamDTO) {
        TestBloodDeviceParamNewDO deviceParamNewDO = testBloodDeviceParamNewMapper.selectOne(
                Wrappers.<TestBloodDeviceParamNewDO>lambdaQuery()
                        .eq(TestBloodDeviceParamNewDO::getDeviceSno, testDeviceParamDTO.getDeviceSno())
        );
        TestDeviceParamVO testDeviceParamVO = new TestDeviceParamVO();
        if (Validator.isNull(deviceParamNewDO)) {
            throw new BizException(610, "根据设备号查询没有查询到云端数据,请联系技术人员");
        }
        
        if (ObjectUtil.isNotEmpty(testDeviceParamDTO.getDeviceSha())) {
            if (testDeviceParamDTO.getDeviceSha().equals(deviceParamNewDO.getDeviceSha())) {
                testDeviceParamVO.setDeviceSha(deviceParamNewDO.getDeviceSha());
                testDeviceParamVO.setDeviceSno(deviceParamNewDO.getDeviceSno());
                return testDeviceParamVO;
            }
        }
        testDeviceParamVO.setDeviceParamData(deviceParamNewDO.getDeviceParamData());
        testDeviceParamVO.setDeviceSha(deviceParamNewDO.getDeviceSha());
        testDeviceParamVO.setDeviceSno(deviceParamNewDO.getDeviceSno());
        return testDeviceParamVO;
    }

    @Override
    public int deleteBySno(String sno) {
        return testBloodDeviceParamNewMapper.deleteBySno(sno);
    }
}
